﻿using CoreModule;
using Prism.Navigation;
using Prism.Regions;
using System.Windows.Controls;
namespace IdResourceModule.Views
{
    /// <summary>
    /// Interaction logic for MeasurementResultManagement
    /// </summary>
    public partial class ManagementHome : UserControl, IRegionMemberLifetime, IDestructible
    {
        IDeviceManager DeviceManager { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="windowLifetime"></param>
        /// <param name="deviceManager"></param>
        public ManagementHome( IWindowLifetime windowLifetime, IDeviceManager deviceManager)
        {
            InitializeComponent();
            windowLifetime.RegionViewKind = RegionViewKind.ManagementHome;
            DeviceManager = deviceManager;
            DeviceManager.MeasurementResultCompleted += DeviceManager_MeasurementResultCompleted;
        }
        /// <summary>
        /// 收到测量数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void DeviceManager_MeasurementResultCompleted(object sender, MeasurementResultInfoEventArgs e)
        {
            DeviceManager.SubmitResult(new ComposeResultInfoEventArgs(new RecognitionResultInfo()
            { 
                PatientInfo = CoreModule.PatientInfo.Instance
            }, e.MeasurementResultInfo));
           if ( ucMeasurementList.DataContext is ManagementModule.ViewModels.MeasurementListViewModel vmMeasurementList)
            {
                vmMeasurementList.ResetPaginationPageIndex();
                vmMeasurementList.UpdateStatistic();
                await vmMeasurementList.Query(1);
              
            }
            if (ucPatientList.DataContext is ManagementModule.ViewModels.PatientListViewModel vmPatientList)
            {
                vmPatientList.ResetPaginationPageIndex();
                await vmPatientList.Query(1);
            }
            DeviceManager.SendMeasurementResultCompletedInManagementModule(sender, e);
            CoreModule.PatientInfo.Instance = null;
        }
        public bool KeepAlive => false;
        public void Destroy()
        {
            DeviceManager.MeasurementResultCompleted -= DeviceManager_MeasurementResultCompleted;
        }
    }
}
